package cn.jnetty.ioc.scan;

import cn.jnetty.utils.create.JnettyResourceScanning;
import cn.jnetty.utils.create.Resource;

/**
 * @author 猪哥亮
 * @email 1030681978@qq.com
 * @date 2020/4/20 11:53
 * 描述：
 */
public class JnettyIocResourceScanning {
    private static final String PACKAGE_INFO_CLASS = "package-info.class";
    private static final String CLASS_SUFFIX = ".class";
    public static void iocLoader(String... packages) {
        JnettyResourceScanning.scanPackage(packages, (resource) -> analysisResource(resource));
        AnnotationHandler.autowired();
    }
    private static void analysisResource(Resource resource) {
        String resourceName = resource.getName();

        // 忽略 package-info.class
        if (PACKAGE_INFO_CLASS.equals(resourceName)) {
            return;
        }

        // 忽略非Class文件
        if (!resourceName.endsWith(CLASS_SUFFIX)) {
            return;
        }
        // Class快速载入
        AnnotationHandler.autowired(ClassUtils.loadClass(resourceName.substring(0, resourceName.length() - 6).replaceAll("[/\\\\]", ".")));
    }
}
